package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserCapital;
import cn.net.shuxing.yifu.common.bean.UserPassword;
import cn.net.shuxing.yifu.common.bean.UserRealInfo;
import cn.net.shuxing.yifu.common.constant.CodeConst;
import cn.net.shuxing.yifu.common.constant.UserParamConst;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.MD5Util;
import cn.net.shuxing.yifu.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;

@Service
public class FrontCommonService {

    @Autowired
    private UserPasswordService userPasswordService;

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private UserService userService;

    @Autowired
    private DealParamService dealParamService;

    @Autowired
    private UserRealInfoService userRealInfoService;

    /**
     * 查询资金密码是否必须
     *
     * @param userId
     * @return
     */
    public boolean isCapitalPasswordRequired(Integer userId) {
        UserPassword userPassword = userPasswordService.getByPrimaryKey(userId);
        if (userPassword == null) {
            return false;
        }
        Integer required = userPassword.getRequired();
        return required == 1;
    }

    public boolean verifyCapitalPassword(Integer userId, String capitalPassword) {
        UserPassword userPassword = userPasswordService.getByPrimaryKey(userId);
        if (userPassword == null) {
            return false;
        }
        if (!StringUtils.hasText(capitalPassword))
            return false;
        return capitalPassword.equals(userPassword.getCapitalPassword());
    }

    public boolean verifyPassword(Integer userId, String password) {
        User user = userService.getByPrimaryKey(userId);
        if (password == null ||user == null) {
            return false;
        }
        return MD5Util.md5(password).equals(user.getPassword());
    }

    /**
     * 验证交易key
     *
     * @param userId
     * @param key
     * @return
     */
    public boolean verifyTransferKey(Integer userId, String key) {
        if (key == null)
            return false;
        //Jedis //jedis = jedisUtil.getJedis();
        String value = jedisUtil.get(key);
        //jedisUtil.close();
        if (!StringUtils.hasText(value))
            return false;
        boolean flag = isCapitalPasswordRequired(userId);
        if (flag)
            return value.equals("3");
        return value.equals("2");
    }

    public boolean isWalletLeftCountEnough(Integer userId, int coinTypeId, BigDecimal count) {
        UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeId(userId, coinTypeId);
        if (userCapital == null) {
            return false;
        }
        BigDecimal leftCount = userCapital.getAliveCoinCount();
        if (leftCount.compareTo(count) >= 0) {
            return true;
        }
        return false;
    }

    public boolean isWalletLeftCountEnoughMinKeep(Integer userId, int coinTypeId, BigDecimal count) {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        if (user.getType() == UserType.SHAREHOLDER) {
            UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeId(userId, coinTypeId);
            if (userCapital == null) {
                return false;
            } else {
                BigDecimal userCapitalAliveCoinCount = userCapital.getAliveCoinCount();
                if (count.compareTo(userCapitalAliveCoinCount) > 0) {
                    return false;
                }
            }
            return true;
        }
        if (user.getType() == UserType.MEMBER) {
            UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeId(userId, coinTypeId);
            if (userCapital == null) {
                return false;
            }
            BigDecimal leftCount = userCapital.getAliveCoinCount();
            String value = dealParamService.getValue(userId, coinTypeId, UserParamConst.MEMBER_KEEP_ALIVE_COIN_MIN_COUNT);
            if (!StringUtils.hasText(value)) {
                System.out.println("币种:" + coinTypeId + " 未设置 " + UserParamConst.MEMBER_KEEP_ALIVE_COIN_MIN_COUNT);
                return false;
            }
            BigDecimal dealWalletMinCount = new BigDecimal(value);
            if (leftCount.subtract(count).compareTo(dealWalletMinCount) < 0)
                return false;
            return true;
        }
        return false;

    }

    public boolean isWalletLeftCountEnoughMaxKeep(Integer userId, int coinTypeId, BigDecimal count) {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);

        if (user.getType() == UserType.SHAREHOLDER) {
            UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeId(userId, coinTypeId);
            if (userCapital == null) {
                return false;
            }
            return true;
        }
        if (user.getType() == UserType.MEMBER) {
            UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeId(userId, coinTypeId);
            if (userCapital == null) {
                return false;
            }
            BigDecimal leftCount = userCapital.getAliveCoinCount();
            String value = dealParamService.getValue(userId, coinTypeId, UserParamConst.MEMBER_KEEP_ALIVE_COIN_MAX_COUNT);
            if (!StringUtils.hasText(value)) {
                System.out.println("币种:" + coinTypeId + " 未设置 " + UserParamConst.MEMBER_KEEP_ALIVE_COIN_MAX_COUNT);
                return false;
            }
            BigDecimal dealWalletMinCount = new BigDecimal(value);
            if (leftCount.add(count).compareTo(dealWalletMinCount) > 0)
                return false;
            return true;
        }
        return false;

    }

    public boolean isTrueInfoVerified(Integer userId) {
        System.out.println("userId" + userId);
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        System.out.println("userRealInfo" + userRealInfo);
        boolean flag = true;
        if (userRealInfo == null || userRealInfo.getVerified() == 0)
            flag = false;
        return flag;
    }
}
